package com.diver.flinkdemo.service.impl;

import com.diver.flinkdemo.entity.xxt.IssuedPermissionRecord;
import com.diver.flinkdemo.mapper.dorisMapper.IssuedPermissionRecordMapper;
import com.diver.flinkdemo.service.IIssuedPermissionRecordService;
import com.diver.flinkdemo.utils.DynamicDataSourceUtil;
import lombok.extern.slf4j.Slf4j;
import org.apache.ibatis.session.SqlSession;

/**
 * Created with IntelliJ IDEA.
 *
 * @Description:
 * @Author: zhangB
 * @Date: 2023/3/8 14:47
 */
//@Service
@Slf4j
public class IssuedPermissionRecordServiceImpl implements IIssuedPermissionRecordService {

    IssuedPermissionRecordMapper issuedPermissionRecordMapper;
    SqlSession session = null;

    @Override
    public IssuedPermissionRecord getByTagId(String tagId) {
        try {
            session = DynamicDataSourceUtil.dorisSessionFactory.openSession();
            issuedPermissionRecordMapper = session.getMapper(IssuedPermissionRecordMapper.class);
            return issuedPermissionRecordMapper.selectById(tagId);
        } finally {
            endOfSession(session);
        }
    }

    @Override
    public void saveIssuedPermissionRecord(IssuedPermissionRecord record) {
        try {
            session = DynamicDataSourceUtil.dorisSessionFactory.openSession();
            issuedPermissionRecordMapper = session.getMapper(IssuedPermissionRecordMapper.class);
            issuedPermissionRecordMapper.insert(record);
        } finally {
            endOfSession(session);
        }
    }


    public void endOfSession(SqlSession session) {
        //关闭sqlSession
        if (session != null) {
            log.info("session commit -> session close");
//            session.commit();
            session.close();

        }
    }
}
